package com.lanou3g.myapplication.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou3g.myapplication.DaoSingleton;
import com.lanou3g.myapplication.R;
import com.lanou3g.myapplication.activity.singleton.activity.DetalisImageActivity;
import com.lanou3g.myapplication.adapter.RecommendedAdapter;
import com.lanou3g.myapplication.bean.ResponseEty;
import com.lanou3g.myapplication.activity.singleton.VolleySingleton;
import com.lanou3g.myapplication.bean.BeanData;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class FragmentBeauty extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private ListView listView;
    private RecommendedAdapter adapter;
    // private RequestQueue requestQueue;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/1404457531633?client_os=android&client_version=2.1.0.1&bid=800&m_ch=004&city=0411&sn=ade627ae9c8b3165f8beab83650296fd&content_cnt=20&recoid=&ftime=0&method=new&count=20";
    private List<BeanData> data;
    private SwipeRefreshLayout swipeRefreshLayout;
    // private VolleySingleton volleySingleton;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recommended_fragment, null);
        listView = (ListView) view.findViewById(R.id.listview_fragment_lv);
        //listView.setOnItemClickListener(this);
        //VolleySingleton.getInstance();
        // requestQueue = Volley.newRequestQueue(getActivity());
        VolleySingleton.getInstance();
        DaoSingleton.getInstance();
        Data();
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.listview1_fragment_srl);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        return view;
    }


    public void Data() {

        VolleySingleton.addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                data = new ArrayList<>();
                Gson gson = new Gson();
                ResponseEty responseEty = gson.fromJson(response, ResponseEty.class);
                Log.e("*****", responseEty.getData().getArticle().size() + "");
                for (int i = 0; i < responseEty.getData().getArticle().size(); i++) {
                    BeanData beanData = new BeanData();
                    beanData.setTitle(responseEty.getData().getArticle().get(i).getTitle());
                    beanData.setContent_type(responseEty.getData().getArticle().get(i).getContent_type());
                    beanData.setItem_type(responseEty.getData().getArticle().get(i).getItem_type());
                    beanData.setImageSize(responseEty.getData().getArticle().get(i).getImage().size());
                    Log.d("FragmentBeauty", "imageSize:" + responseEty.getData().getArticle().get(i).getImage().size());
                    for (int a = 0; a < responseEty.getData().getArticle().get(i).getImage().size(); a++) {
                        beanData.setImageUrl(responseEty.getData().getArticle().get(i).getImage().get(0).getUrl());
                        beanData.setDetailsImageUrl(responseEty.getData().getArticle().get(i).getImage().get(a).getUrl());
                        beanData.setIndex(responseEty.getData().getArticle().get(i).getImage().get(a).getIndex());
                        Log.d("FragmentBeauty", "responseEty.getData().getArticle().get(i).getImage().get(a).getIndex():" + "*****" + i + "****" + responseEty.getData().getArticle().get(i).getImage().get(a).getUrl());
//                    data=new ArrayList<>();

                    }
                   // Log.d("FragmentBeauty", "responseEty.getData().getArticle().get(i).getImage().size():" + responseEty.getData().getArticle().size());

                    adapter = new RecommendedAdapter(data, getActivity());
                    listView.setAdapter(adapter);
                    data.add(beanData);
                }
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Toast.makeText(getActivity(), "position:" + position, Toast.LENGTH_SHORT).show();
                        int num = position;
                        Intent intent = new Intent(getActivity(), DetalisImageActivity.class);
                        intent.putExtra("position", num);
                        intent.putExtra("data", (Serializable) data);

                        startActivity(intent);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                Data();
            }
        }, 3000);
    }

}
